
package kaizone.haining.chaobohui.widget;

import java.io.File;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.util.FileWorker;
import kaizone.haining.chaobohui.util.ImageFetcher;
import kaizone.haining.chaobohui.util.ImageResizer;
import kaizone.haining.chaobohui.util.ImageUtil;
import kaizone.haining.chaobohui.util.Utils;
import kaizone.haining.chaobohui.util.FileWorker.OnDownLoadListener;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class TushuoHead extends RelativeLayout implements OnClickListener {

    private TextView mBackView;

    private TextView mTextView;

    private TextView mDownloadView;

    private FileWorker mFileWorker;

    public String mDownurl;

    public TushuoHead(Context context) {
        super(context);
        init();
    }

    public TushuoHead(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mFileWorker = new FileWorker(getContext());
        mFileWorker.setOnLoadDownListener(new LoadPictureLintener());
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mBackView = (TextView) findViewById(R.id.img01);
        mBackView.setOnClickListener(this);
        mTextView = (TextView) findViewById(R.id.tv01);
        mDownloadView = (TextView) findViewById(R.id.img02);
        mDownloadView.setOnClickListener(this);

        Drawable d1 = Utils.scaleDrawable(getContext(), R.drawable.btn_backwhite, 0.6f);
        Drawable d2 = Utils.scaleDrawable(getContext(), R.drawable.btn_download, 0.6f);
//
        mBackView.setCompoundDrawables(d1, null, null, null);
        mDownloadView.setCompoundDrawables(null, null, d2, null);
        
        mBackView.setMaxHeight(d1.getIntrinsicHeight());
        mBackView.setMaxWidth(d1.getIntrinsicWidth());

    }

    public void setText(String text) {
        mTextView.setText(text);
    }

    public String getDownurl() {
        return mDownurl;
    }

    public void setDownurl(String downurl) {
        this.mDownurl = downurl;
    }

    @Override
    public void onClick(View v) {
        if (v == mBackView) {
            if (getContext() instanceof Activity) {
                ((Activity) getContext()).finish();
            }
        }
        //
        else if (v == mDownloadView) {
            if (TextUtils.isEmpty(mDownurl))
                return;
            mFileWorker.loadFile(mDownurl);
        }

    }

    public void loadPrompt(String msg) {
        Toast.makeText(getContext(), msg, 300).show();
    }

    class LoadPictureLintener implements OnDownLoadListener {

        @Override
        public void OnStart() {
            loadPrompt("开始下载");
        }

        @Override
        public void onUpateProgress(Object obj, int progress) {
            // TODO Auto-generated method stub

        }

        @Override
        public void OnExcetption(String url) {
            loadPrompt("下载出错");
        }

        @Override
        public void OnComplete(String url) {
            StringBuilder sb = new StringBuilder();
            String dir = mFileWorker.getFileCache().getCacheDir().getAbsolutePath();
            String name = FileWorker.obianName(url);
            sb.append(dir).append(File.separator).append(name);
            loadPrompt("下载完成，保存至" + sb);
        }
    }

}
